﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using PayRoller.Include;
using HySoft.Cash;

namespace PayRoller.Manage.Documents
{
    public class DocAdd : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Button bnUpload;
        protected FileUpload FileUpload1;
        protected HtmlForm form1;
        protected Label lblTip;
        protected TextBox txtFilename;
        protected TextBox txtPath;
        protected UOrgNameInput UOrgNameInput1;

        // Methods
        protected void bnUpload_Click(object sender, EventArgs e)
        {
            if ((this.FileUpload1.PostedFile.FileName != null) && (this.FileUpload1.PostedFile.FileName != ""))
            {
                string fileExtName = "";
                int num = this.FileUpload1.PostedFile.FileName.LastIndexOf(".");
                fileExtName = this.FileUpload1.PostedFile.FileName.Substring(num + 1);
                string fileNewName = "";
                fileNewName = Guid.NewGuid().ToString() + "." + fileExtName;
                this.FileUpload1.PostedFile.SaveAs(base.Server.MapPath("~/" + ConfigurationManager.AppSettings["upPath"] + "/" + fileNewName));
                //this.txtPath.Text = ConfigurationManager.AppSettings["upPath2"] + "/" + str2;
                TdocumentsBN sbn = new TdocumentsBN(this.Page);
                TdocumentsDT detail = new TdocumentsDT();
                string fileNewNameDisplay = this.txtFilename.Text;
                if (fileNewNameDisplay == string.Empty)
                {
                    fileNewNameDisplay= this.FileUpload1.FileName;
                }
                detail.Filename = fileNewNameDisplay;
                detail.Filepath = fileNewName; //文件保存的路径有配置文件决定，数据库内部保存路径，仅保存guid类型的文件名称。
                detail.Pubdate = SafeDateTime.LocalNow;
                detail.OrgID = this.UOrgNameInput1.OrgIDSelected;
                sbn.Add(detail);
                sbn.Close();
                sbn.Dispose();
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Success!');</script>");
                this.txtFilename.Text = "";
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Please select your file!');</script>");
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "0002";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.bnUpload.Attributes.Add("onclick", "return Check();");
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.
                Profile;
            }
        }
    }

}
